<?php
/*Problem Domain
To perform the actual CRUD operations on User by implementing the UserDAO interface
*/
/*
include_once 'UserDAO.php';
include_once '../model/User.php';
include_once '../model/ListUserType.php';
*/
class UserDAOImpl implements UserDAO
{
	private $conn;
	private $array_list;

	/*void 
	purpose. initializes the newly created UserDAOImpl.*/
	public function __construct()
	{
		$this->conn = new mysqli('localhost' , 'root' , 'root123' , 'tms') or
		die('Connect error : '.$this->conn->connect_error);

		$this->array_list = new ListUserType();
	}

	/*void -> Connection
	purpose. produces the connection object*/
	public function getConnection()
	{
		return $this->conn;
	}

	/*User -> boolean
	purpose. produces true if the given User is found , false otherwise*/
	private function found($User)
	{
		$conn = $this->getConnection();
		$email_id = $User->getEmailId();
		$res = $conn->query("select * from User where email_id = '".$email_id."'");
		$status;
		
		if($res->num_rows == 0)
		{
			$status = false;
		}
		else
		{
			$status = true;
		}

		return $status;
	}

	/*User -> boolean
	purpose. To persist the given User into the database and return true on success
	false otherwise*/
	private function save($User)
	{
		$conn 	  = $this->getConnection();
		$email_id = $User->getEmailId();
	   	$password = $User->getPassword();
		$role	  = $User->getRole();
		
		$stmt = $conn->prepare('insert into User values(NULL , NULL , ? , ? , ? )');
		$status = true;

		if(!$stmt)
		{
			echo "\n Prepared Statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('sss', $email_id, $password, $role))
			{
				echo "Bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : moh ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
				else
				{
					$id = $conn->insert_id;
					$User->setId($id);
				}
			}
		}

		return $status;		
	}

	/*User -> boolean
	pupose. To update the already existing given User and return true on success , false otherwise.*/
	public function update($User)
	{
		$conn     = $this->getConnection();
		$user_id  = $User->getId();
		$email_id = $User->getEmailId();
		$password = $User->getPassword();
		$role	  = $User->getRole();	
		$status = true;
		$stmt = $conn->prepare('update User set email_id = ? , password = ? , role = ? where User_id = ?');

		if(!$stmt)
		{
			echo "Prepare statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('sssi', $email_id, $password, $role, $User_id))
			{
				echo "Parameter bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
			}
		}

		return $status;
	}

	/*User -> boolean
        purpose. to save or update a given User into the database and return true on success , 
        false otherwise*/
        public function saveOrUpdate($User)
	{
		$status;
		if(!$this->found($User))
		{
			$status = $this->save($User);			
		}
		else
		{
			$status = $this->update($User);
		}

		return $status;
	}	

	/*User -> boolean
        purpose. removes a given User from the database and returns true
        on succes , false otherwise*/
        public function remove($User)
	{
		$conn = $this->getConnection();
		$status = true;

		if(!$this->found($User))
		{
			echo "\n".$User->getEmaiId()."is not found";
			$status = false;
		}
		else
		{
			$user_id = $User->getId();
			$sql     = "delete from User where user_id = ".$user_id;

			if(!$conn->query($sql))
			{
				echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
				$status = false;
			}
		}

		return $status;
	}
	
	/*void -> LostUserType
	purpose. produces the entire list of Users of the system*/
	public function listUsers()
	{
		$conn = $this->getConnection();
		$sql = 'select * from User';
		$res = $conn->query($sql);

		while($row = $res->fetch_assoc())
		{
			$user_id 	= $row['user_id'];
			$emp_id 	= $row['emp_id'];
			$email_id	= $row['email_id'];
			$password	= $row['password'];
			$role		= $row['role'];
			
			$User		= new User($email_id, $password, $role, $user_id, $emp_id);

			$this->array_list->addToList($User);
		}

		return $this->array_list;
	}
}
 
?>
